package com.mall.config;

import cn.dev33.satoken.stp.StpInterface;
import cn.dev33.satoken.strategy.SaStrategy;
import com.aventrix.jnanoid.jnanoid.NanoIdUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mall.pojo.Admin;
import com.mall.service.IAdminService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description
 * @ClassName SaTokenConfigure
 * @Author cxx
 * @Date 2023/6/7 20:44
 * @Version 1.0
 */
@Configuration
@Slf4j
public class SaTokenConfigure implements StpInterface {
    /**
     * 重写 Sa-Token 框架内部token值算法策略
     */
    @Autowired
    public void rewriteSaStrategy() {
        // 重写 Token 生成策略
        SaStrategy.me.createToken = (loginId, loginType) -> {
            return NanoIdUtils.randomNanoId();    //  NanoId随机生成
        };
    }

    @Resource
    private IAdminService adminService;

    /**
     * 根据用户ID赋予登录用户不同权限
     */

    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {
        ArrayList<String> list = new ArrayList<>();
        LambdaQueryWrapper<Admin> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Admin::getId, loginId);
        Admin admin = adminService.getOne(wrapper);
        if (ObjectUtils.isNotEmpty(admin) && admin.getIsSuper() == 1) {
            list.add("user-delete");
            list.add("user-update");
            list.add("user-save");
            list.add("user-query");
            list.add("admin-delete");
            list.add("admin-update");
            list.add("admin-save");
            list.add("admin-query");
            list.add("ware-delete");
            list.add("ware-update");
            list.add("ware-save");
            list.add("ware-query");
        } else if (ObjectUtils.isNotEmpty(admin) && admin.getIsSuper() != 1) {
            list.add("user-delete");
            list.add("user-update");
            list.add("user-save");
            list.add("user-query");
            list.add("ware-delete");
            list.add("ware-update");
            list.add("ware-save");
            list.add("ware-query");
        } else {
            list.add("mine-update");
            list.add("ware-query");
        }
        return list;
    }

    /**
     * 赋予不同用户角色
     */

    @Override
    public List<String> getRoleList(Object loginId, String loginType) {
        ArrayList<String> list = new ArrayList<>();
        LambdaQueryWrapper<Admin> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Admin::getId, loginId);
        Admin admin = adminService.getOne(wrapper);
        if (ObjectUtils.isNotEmpty(admin) && admin.getIsSuper() == 1) {
            list.add("super-admin");
            list.add("admin");

        } else if (ObjectUtils.isNotEmpty(admin) && admin.getIsSuper() != 1) {
            list.add("admin");

        } else {
            list.add("user");
        }
        return list;
    }


}
